package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Cuser;
import com.zr.service.InsertUserService;

@Controller
public class InsertUserAction extends ActionSupport implements ServletResponseAware, SessionAware, ServletRequestAware {
	private HttpServletResponse resp;
	private Map<String, Object> session;
	private HttpServletRequest req;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	@Resource
	private InsertUserService insertUserService;

	public void insertUser() {
		String uroot = req.getParameter("uroot");
		String upassword = req.getParameter("upassword");
		String uname = req.getParameter("uname");
		String utel = req.getParameter("utel");
		String rname = req.getParameter("rname");
		String question = req.getParameter("question");
		String answer = req.getParameter("answer");
		int disid = 9527008;
		try {
			PrintWriter pw = resp.getWriter();
			if (insertUserService.getUrootExistsOrNot(uroot) > 0) {
				pw.write("2");
			} else {
				if ("".equals(uroot)) {
					pw.write("3");
				} else if ("".equals(upassword)) {
					pw.write("4");
				} else if ("".equals(uname)) {
					pw.write("5");
				} else if ("".equals(utel)) {
					pw.write("6");
				} else if ("".equals(rname)) {
					pw.write("7");
				} else if ("".equals(question)) {
					pw.write("8");
				} else if ("".equals(answer)) {
					pw.write("9");
				} else {
					Cuser currentUser = (Cuser)session.get("cuser");
					String cuname = currentUser.getUname();
					int cuid = currentUser.getUid();
					int rid = insertUserService.getRidByRname(rname);
					insertUserService.insertUserByInfo(uroot, upassword, uname, utel, rid, question, cuid, cuname, answer, disid);;
					pw.write("1");
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
